<?php

include 'queryengine.php';

class Alarm
{ 
	private $alarmFile , $alarmTime;
	
	private $alarmProperties = array();
	
	# The alarmProperties array holds all data on an Alarm instance in the form:
	#
	# 1. Name
	# 2. Time
	# 3. Alarm File (the sound file)
	# 
	
	function outputAlarmProperties()
	{
		echo $this->$alarmProperties;
	}
	
	function setName($newName)
	{
		$this->$alarmProperties[1] = $newName;
	}
	
	function setTime($newTime)
	{
		if(strlen($newTime) == 4)
		{
			$this->$alarmProperties[2] = $newTime;
		}
		else
		{
			throw new Exception('function expects a 4 digit string in 24 hour format');
		}
	}
	
	function setAlarmFile($newAlarmFile)
	{
		$this->$alarmProperties[3] = $newAlarmFile;
	}
	
	function playAlarm()
	{
		#Alarm initialises over audio
	}
	
}

class Clock
{
	private $today,$hour,$minute,$second;

	function showDate($format)
	{
		$today = getdate();
		$counter = 0;
		
		if ( $format == 1 )
		{
			foreach ($today as $dateSection)
			{
				$counter++;
				$dateSelectionName = 0;
				
				switch($counter)
				{
					case 1: $dateSelectionName = "Seconds"; break;
					case 2: $dateSelectionName = "Minutes"; break;
					case 3: $dateSelectionName = "Hours"; break;
					case 4: $dateSelectionName = "Day of Month"; break;
					case 5: $dateSelectionName = "Week"; break;
					case 6: $dateSelectionName = "Month"; break;
					case 7: $dateSelectionName = "Year"; break;
					case 8: $dateSelectionName = "Day of Year"; break;
					case 9: $dateSelectionName = "Name of Day"; break;
					case 10: $dateSelectionName = "Name of Month"; break;
					case 11: $dateSelectionName = "Unix"; break;
				}
				echo $dateSelectionName . ": " . $dateSection . "<br/>";
			
				#if ( $format == 2 )
				#{
				#	$counter++;
				#	$outputArray = new Array();
				#	
				#	switch( $counter )
				#	{
				#		case 1: $dateSelection; break; #Seconds
				#		case 2: $dateSelection; break; #Minutes
				#		case 3: $dateSelection; break; #Hours
				#		case 4: $dateSelection; break; #Day of Month
				#		case 5: $dateSelection; break; #Week
				#		case 6: $dateSelection; break; #Month
				#		case 7: $dateSelection; break; #Year
				#		case 8: $dateSelection; break; #Day of Year
				#		case 9: $dateSelection; break; #Name of Day
				#		case 10: $dateSelection; break; #Name of Month
				#		case 11: $dateSelection; break; #Unix
				#	}
				#	
				#	echo
				#	
				#}
			}

		}
		
		if( $format == 2 )
		{
			echo takeSpace($today["hours"]) . ":" . takeSpace($today["minutes"]) . ":" . takeSpace($today["seconds"]);
		}
	}
}

function takeSpace($inString)
{
	if( strlen($inString) < 2 )
	{
		return "0" . $inString;
	}
	else
	{
		return $inString;
	}
}

function newAlarm($alarmName,$alarmTime)
{
	return crowFather("insert into alarm(alarmName,alarmTime) values ('" . $alarmName . "' , '" . $alarmTime . "');",3);
}

function generateListAlarms($searchTerm = null)
{
	#Specify a $searchTerm if you are looking for a specific AlarmName
	#Lists all alarms in an list for use on a list box.
	
	#add ID as value
	if($searchTerm == null)
	{
		$alarmArray = crowFather('select alarmID,alarmName,alarmTime from alarm;',1);
	}
	else
	{

		$alarmArray = crowFather('select alarmID,alarmName,alarmTime from alarm where alarmName like ("%' . $searchTerm . '%";' ,1);
	}
	
	$htmlOutput = "";
	
	foreach ($alarmArray as $row)
	{
		$loopPass = 0;
		$htmlOutput .= "<option class='selectBoxOption' ";
		
		foreach ($row as $cell)
		{
			#add ID here
			switch ( $loopPass )
			{
				case 1: $htmlOutput .= " value=" . $cell; break; #Alarm ID
				case 3: $htmlOutput .= " title=" . $cell . " data-alarmName=" . $cell ; $alarmName =  $cell; break; #Alarm Name
				case 5: $htmlOutput .= " data-alarmtime=" . $cell . ">" . $alarmName . " - " . $cell;break; #Alarm Time
			}
			$loopPass ++;
		}
		$htmlOutput .= "</option>";
	}
	return $htmlOutput;
}

?>